<?php

abstract class Lib_SMS_Base_Abstract
{
    public static $error_message = [];
    const TIME_OUT = 28; //请求超时
    protected $config = array();
    //必须实现的方法
    abstract public function sendCode($mobile, $content);

    abstract public function sendSms($mobile, $content, $sign);

    public function setConfig($config)
    {
        $this->config = $config;
        return $this;
    }


    public function curlPost($url, $params ,$is_login = 0)
    {
        $params = json_encode($params,JSON_UNESCAPED_UNICODE);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array (
                "Accept:application/json",
                'Content-Type:application/json',
                "Content-Length:" . strlen($params)
            )
        );
        curl_setopt($ch, CURLOPT_POSTFIELDS,$params );
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $res = curl_exec($ch);
        //请求超时
        if ($res == false && curl_errno($ch) == self::TIME_OUT) {
            curl_close($ch);
            return self::TIME_OUT;
        }
        curl_close($ch);

        return $res;
    }

    public function curlGet($url, $params)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $res = curl_exec($ch);
        //请求超时
        if ($res == false && curl_errno($ch) == self::TIME_OUT) {
            curl_close($ch);
            return self::TIME_OUT;
        }
        curl_close($ch);
        return $res;
    }

}